package com.burgeon.backend.EnterpriseWeChat.rest.vip;

import com.burgeon.backend.EnterpriseWeChat.entity.Retail;
import com.burgeon.backend.EnterpriseWeChat.entity.VipDeposit;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GetVipDeposit extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public GetVipDeposit(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<VipDeposit> getData(String empId, String storeId,String startDate, String endTime, int page) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(empId,storeId,startDate,endTime,page), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<VipDeposit> vipDeposits = new ArrayList<>();

        if(jsonArray.length()>0){
            for (int i = 0; i < jsonArray.length(); i++) {
                VipDeposit vipDeposit = new VipDeposit();
                vipDeposit.setVipName(jsonArray.getJSONArray(i).getString(0));
                vipDeposit.setType(jsonArray.getJSONArray(i).getString(1));
                vipDeposit.setVipCardNo(jsonArray.getJSONArray(i).getString(2));
                vipDeposit.setEmpName(jsonArray.getJSONArray(i).getString(3));
                vipDeposit.setStoreName(jsonArray.getJSONArray(i).getString(4));
                vipDeposit.setBillDate(jsonArray.getJSONArray(i).getString(5));
                vipDeposit.setDepositAmt(jsonArray.getJSONArray(i).getString(6));
                vipDeposit.setGiveAmt(jsonArray.getJSONArray(i).getString(7));

                vipDeposits.add(vipDeposit);
                System.err.println(vipDeposit);
            }
        }
        return vipDeposits;
    }

    private MultiValueMap Data(String empId,String storeId,String startDate,String endDate,int page) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("'C_VIP_ID;VIPNAME'");//会员名
        colums.add("'TYPE'");//充值类型
        colums.add("'C_VIP_ID;CARDNO'");//卡号
        colums.add("'SALESREP_ID;NAME'");//导购
        colums.add("'C_STORE_ID;NAME'");//店仓
        colums.add("'BILLDATE'");//日期
        colums.add("'TOT_AMT_DEPOSIT'");//充值金额
        colums.add("'TOT_AMT_GIVE'");//赠送金额

        params.put("columns", colums);
        params.put("table", "C_PICKINGCARDBORD");//表名
        params.put("range",RANGE);
//        params.put("count","true");
        int r = Integer.valueOf(RANGE);
        params.put("start",String.valueOf((page-1)*r));

        HashMap orderby = new HashMap();
        orderby.put("column","BILLDATE");
        orderby.put("asc","false");

        ArrayList list = new ArrayList();
        list.add(orderby);
        params.put("orderby",list);

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        if(empId != null && !empId.isEmpty()) {
            expr1.put("column", "'SALESREP_ID;ID'");
            expr1.put("condition", empId);
        }else {
            expr1.put("column", "'C_STORE_ID'");
            String b =  "'"+storeId+"'";
            expr1.put("condition", b);
        }

        HashMap expr2 = new HashMap();
        String ac =  "'"+startDate+"~"+endDate+"'";
        expr2.put("column", "'BILLDATE'");
        expr2.put("condition", ac);

        paramss.put("combine", "and");
        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);

        params.put("params", paramss);
        System.err.println(params);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetVipDeposit g = new GetVipDeposit("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {//3125
            System.err.println(g.getData("5872","","20201201","20201231",1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
